package com.zhb.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhb.lkd.base.PageResponse;
import com.zhb.lkd.exception.BusinessException;
import com.zhb.lkd.mapper.*;
import com.zhb.lkd.pojos.entity.*;
import com.zhb.lkd.pojos.vo.NodeVo;
import com.zhb.lkd.pojos.vo.RegionVo;
import com.zhb.lkd.pojos.vo.VmListNodeVo;
import com.zhb.lkd.service.INodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.text.Utilities;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 点位表 服务实现类
 * </p>
 *
 * @author haibin
 * @since 2024-08-18
 */
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements INodeService {
    @Resource
    private VendingMachineMapper vendingMachineMapper;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private VmTypeMapper vmTypeMapper;

    /**
     * 查看区域详情
     *
     * @param pageIndex
     * @param pageSize
     * @param regionId
     * @return
     */
    @Override
    public PageResponse search(Integer pageIndex, Integer pageSize, String regionId, String name) {
        //分页查询
        Page<Node> page = lambdaQuery()
                .eq(StrUtil.isNotBlank(regionId), Node::getRegionId, regionId)
                .like(StrUtil.isNotBlank(name), Node::getName, name)
                .page(new Page<>(pageIndex, pageSize));
        //封装返回数据
        List<NodeVo> nodeVos = nodeToNodeVos(page.getRecords());
        return new PageResponse(page, nodeVos);
    }

    /**
     * 将node集合转换成nodevo集合
     *
     * @param nodes
     * @return
     */
    public List<NodeVo> nodeToNodeVos(List<Node> nodes) {
        return nodes.stream().map(node -> nodeToNodeVo(node)).collect(Collectors.toList());
    }

    /**
     * 将node转换成nodevo
     *
     * @param node
     * @return
     */
    public NodeVo nodeToNodeVo(Node node) {
        NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);
        //查询数量
        Long count = vendingMachineMapper.selectCount(Wrappers.<VendingMachine>lambdaQuery().eq(VendingMachine::getNodeId, node.getId()));
        if (count != null) {
            nodeVo.setVmCount(Math.toIntExact(count));
        } else {
            nodeVo.setVmCount(0);
        }
        //查询运营区域
        Region region = regionMapper.selectById(node.getRegionId());
        if (region != null) {
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            //查询数量
            Long nodeCount = getBaseMapper().selectCount(Wrappers.<Node>lambdaQuery().eq(Node::getRegionId, region.getId()));
            if (count != null || count != 0) {
                regionVo.setNodeCount(Math.toIntExact(count));
            } else {
                regionVo.setNodeCount(0);
            }
            regionVo.setName(region.getRegionName());
            nodeVo.setRegion(regionVo);
        }
        //查询商圈
        Business business = businessMapper.selectById(node.getBusinessId());
        nodeVo.setBusinessType(business);
        return nodeVo;
    }

    /**
     * 新增点位
     *
     * @param node
     */
    @Override
    public void create(Node node) {
        //1.检查参数
        if (StrUtil.isBlank(node.getName())) {
            throw new BusinessException(500, "点位名称不能为空");
        }
        //2.保存
        save(node);
    }

    /**
     * 获取点位详情根据点位id查询设备列表
     *
     * @param nodeId
     * @return
     */
    @Override
    public List<VmListNodeVo> findById(String nodeId) {
        //1.检查参数
        if (StrUtil.isBlank(nodeId)) {
            throw new BusinessException(500, "点位id不能为空");
        }
        //2.查询数据
        //准备返回的数据
        List<VmListNodeVo> vmListNodeVos = new ArrayList<>();
        //根据nodeid查询tb_vending_machine表数据
        List<VendingMachine> vendingMachines = vendingMachineMapper.selectList(Wrappers.<VendingMachine>lambdaQuery().eq(VendingMachine::getNodeId, nodeId));
        //将数据封装到vmListNodeVos
        if (vendingMachines != null || !vendingMachines.isEmpty()) {
            //vendingMachines的数据拷贝到VmListNodeVo
            for (VendingMachine vendingMachine : vendingMachines) {
                //vendingMachines的数据拷贝到VmListNodeVo
                VmListNodeVo vmListNodeVo = BeanUtil.copyProperties(vendingMachine, VmListNodeVo.class);
                //根据vmType查询vmType表
                VmType vmType = vmTypeMapper.selectById(vendingMachine.getVmType());
                vmListNodeVo.setType(vmType);
                //添加到vmListNodeVos
                vmListNodeVos.add(vmListNodeVo);
            }
        }
        //因为根据nodeid查询 所以nodeid应该是都一样的统一添加到vmListNodeVos中
        Node node = getById(nodeId);
        NodeVo nodeVo = nodeToNodeVo(node);
        //根据regionId查询node
        List<Node> list = list(Wrappers.<Node>lambdaQuery().eq(Node::getRegionId, nodeVo.getRegion().getId()));
        nodeVo.getRegion().setNodeList(list);
       //将node和Region封装
        for (VmListNodeVo vmListNodeVo : vmListNodeVos) {
            vmListNodeVo.setNode(nodeVo);
            vmListNodeVo.setRegion(nodeVo.getRegion());
        }
        return vmListNodeVos;
    }

    /**
     * 删除合作商
     * @param nodeId
     */
    @Override
    public void deleteById(String nodeId) {
        //1.检查参数
        if (StrUtil.isBlank(nodeId)){
            throw new BusinessException(500,"id不能为空");
        }
        //2.删除
        removeById(nodeId);
    }

    /**
     * 点位数量
     * @return
     */
    @Override
    public Integer nodeCount() {
        //查询点位数量
        long count = count();
        return Math.toIntExact(count);
    }
}
